package com.powergisol.gis.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.powergisol.gis.dao.TabServiceMapper;
import com.powergisol.gis.entity.TabService;
import com.powergisol.gis.service.RemoteService;
import com.powergisol.gis.util.PropertyUtil;
import com.powergisol.gis.vo.TabServiceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Date: 2019/7/31  15:36
 * Description:
 */
@Service
public class RemoteServiceImpl implements RemoteService {
    private static Logger logger = LoggerFactory.getLogger(RemoteServiceImpl.class);
    @Autowired
    private TabServiceMapper tabServiceMapper;
    @Value("${collectionName}")
    private String collectionName;
//    @Value("${file-url}")
//    private String fileUrl;
//    @Value("${style-url}")
//    private String styleUrl;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResponseEntity getServiceFilePath(String name,String uuid) {

        logger.info("获取服务文件地址和样式地址:{}", name);
        TabServiceVo vo = new TabServiceVo();
        vo.setName(name);
        TabService tabService = tabServiceMapper.getTabServiceCondition(vo);
        if(null == tabService){
            logger.info("服务不存在", name);
            return new ResponseEntity(null,HttpStatus.NOT_FOUND);
        }
        if(tabService.getState()!=1 || null == tabService.getFileUrl()){
            logger.info("服务不可用", name);
            return new ResponseEntity(null,HttpStatus.NOT_FOUND);
        }
        if(null == tabService.getFileUrl() || null == tabService.getStyleUrl()){
            logger.info("服务文件或样式文件不存在", name);
            return new ResponseEntity(null,HttpStatus.NOT_FOUND);
        }

        //增加访问次数
        if(null != uuid && !redisTemplate.hasKey(uuid)){
//            vo.setVisitTimes(tabService.getVisitTimes()+1);
//            vo.setId(tabService.getId());
            tabService.setVisitTimes(tabService.getVisitTimes()+1);
            tabServiceMapper.updateByPrimaryKeySelective(tabService);
        }
        JSONObject json =new JSONObject();
        json.put("code",200);
        if(3 == tabService.getServiceType()){
            json.put("fileUrl",tabService.getFileUrl().replace(".json",".tif"));
        }else{
            json.put("fileUrl",tabService.getFileUrl());
        }

        if(null != tabService.getStyleUrl()){
            String userDir = System.getProperty("user.dir");
            userDir = userDir.substring(0,userDir.lastIndexOf(File.separator));
            json.put("styleUrl",userDir+File.separator+"serviceData"+File.separator+"style"+File.separator+tabService.getStyleUrl());
        }
        if(null != tabService.getTitle()){
            json.put("encodingType",tabService.getTitle());
        }
        logger.info("{}服务结果:{}", name,json);
        return ResponseEntity.ok(json);
    }

    @Override
    public Map<String, Object> countServicceType(String userId,String userName) {
        List<Map<String, Object>> list = tabServiceMapper.countServicceType(userId,userName);
        Integer yxNum = 0;
        Integer gcNum = 0;
        Integer slNum = 0;
        Integer swNum = 0;
        for (Map<String, Object> map:list) {
            if(null == map.get("serviceType")){
                continue;
            }
            Integer serviceType = Integer.valueOf(map.get("serviceType").toString());
            Integer num = Integer.valueOf(map.get("num").toString());
            switch (serviceType){
                case 1:
                    yxNum = yxNum + num;
                    break;
                case 2:
                case 3:
                    gcNum = gcNum + num;
                    break;
                case 4:
                    slNum = slNum + num;
                    break;
                case 5:
                case 6:
                case 7:
                case 8:
                    swNum = swNum + num;
                    break;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("yxNum",yxNum);
        map.put("gcNum",gcNum);
        map.put("slNum",slNum);
        map.put("swNum",swNum);
        return map;
    }

}
